package com.ljb.main.v;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hikvision.vmsnetsdk.CameraInfo;
import com.hikvision.vmsnetsdk.RegionInfo;
import com.ljb.haikanglibrary.HaiKangManager;
import com.ljb.haikanglibrary.callbacks.MsgCallback;
import com.ljb.haikanglibrary.utils.JsonKit;
import com.ljb.haikanglibrary.utils.L;
import com.ljb.haikanglibrary.utils.SP;
import com.ljb.haikanglibrary.utils.ThreadUtils;
import com.ljb.main.CameraAdapter;
import com.ljb.main.DataTemp;
import com.ljb.main.R;
import com.ljb.main.RegionAdapter2;

import java.util.List;

/**
 * Created by ljb on 2017/6/1.
 */

public class SelectDeviceActivity extends BaseActivity {

    private ListView lv_region;
    private ListView lv_camera;
    private Context c = this;
    private List<RegionInfo> regionInfoList;
    private List<CameraInfo> cameraInfos;
    private CameraAdapter cameraAdapter;
    private RegionAdapter2 regionAdapter;

    public static void actionStart(Context c) {
        c.startActivity(new Intent(c, SelectDeviceActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_device);

        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SP.putString(this, "cameraInfoList_my", JsonKit.toJson(DataTemp.init().cameraInfoList_my));

    }

    private void initView() {
        lv_region = (ListView) findViewById(R.id.lv_region);
        lv_camera = (ListView) findViewById(R.id.lv_device);
    }

    private void initData() {
        regionInfoList = DataTemp.init().regionInfoList;
        regionAdapter = new RegionAdapter2(c, regionInfoList);
        lv_region.setAdapter(regionAdapter);
        lv_region.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int regionID = regionAdapter.getItem(position).regionID;
                getCameraData(regionID);
                view.setBackgroundColor(Color.parseColor("#33B5E5"));

                View forView = regionAdapter.getForView();
                forView.setBackgroundColor(Color.WHITE);

                regionAdapter.setForView(view);
            }
        });

        getCameraData(regionInfoList.get(0).regionID);

    }

    private void getCameraData(final int regionID) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                HaiKangManager.init().requestSubResFromRegion(regionID, new MsgCallback() {
                    @Override
                    public void onMsg(int msgId, Object data) {
                        cameraInfos = (List<CameraInfo>) data;
                        //显示监控点列表
                        showCameraList();
                    }
                });
            }
        });
    }

    private void showCameraList() {
        ThreadUtils.runUIThread(new Runnable() {
            @Override
            public void run() {
                cameraAdapter = new CameraAdapter(c, cameraInfos);
                lv_camera.setAdapter(cameraAdapter);
                lv_camera.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        setItemState(view, position);
                    }
                });
            }
        });
    }

    private void setItemState(View view, int position) {
        CameraInfo cameraInfo = cameraAdapter.getItem(position);
        String cameraID = cameraInfo.cameraID;
        String deviceID = cameraInfo.deviceID;

        List<CameraInfo> cameraInfoList = DataTemp.init().cameraInfoList_my;
        for (CameraInfo info : cameraInfoList) {
            if (TextUtils.equals(cameraID, info.cameraID) && TextUtils.equals(deviceID, info.deviceID)) {
                view.setBackgroundColor(Color.WHITE);
                DataTemp.init().cameraInfoList_my.remove(info);
                L.e(JsonKit.toJson(DataTemp.init().cameraInfoList_my));
                return;
            }
        }

        view.setBackgroundColor(Color.parseColor("#33B5E5"));
        DataTemp.init().cameraInfoList_my.add(cameraInfo);
        L.e(JsonKit.toJson(DataTemp.init().cameraInfoList_my));
    }
}
